package com.cuit.bysj.action;

import com.cuit.bysj.entities.Appraise;
import com.cuit.bysj.entities.User;
import com.cuit.bysj.entities.adminResults;
import com.cuit.bysj.service.AppraiseService;
import com.cuit.bysj.utils.JsonHelper;
import com.cuit.bysj.utils.Result;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AppraiseManageAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String result;
	private AppraiseService appraiseService;
	private String id;
	private String reply;
	private String major;
	private String banji;
	private String cname;

	public String getMajor() {
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}

	public String getBanji() {
		return banji;
	}

	public void setBanji(String banji) {
		this.banji = banji;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	public String getReply() {
		return reply;
	}

	public void setReply(String reply) {
		this.reply = reply;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public AppraiseService getAppraiseService() {
		return appraiseService;
	}

	public void setAppraiseService(AppraiseService appraiseService) {
		this.appraiseService = appraiseService;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String openpage() {
		Result<User> Userresult = new Result<User>();
		Userresult.setSuccess(true);

		User user = (User) ActionContext.getContext().getSession().get("User");
		if (user.getRole().getRoleName().equals("管理员")) {
			Userresult.setUrl("AdminTcherlistPage");
		}
		if (user.getRole().getRoleName().equals("学生")) {
			Userresult.setUrl("AppraiseList");
		}
		if (user.getRole().getRoleName().equals("教师")) {
			Userresult.setUrl("ClassList");
		}

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}

	@SuppressWarnings("unchecked")
	public String loadAppraiseList() {
		Result<User> Userresult = new Result<User>();

		User user = (User) ActionContext.getContext().getSession().get("User");

		Userresult.setItems(appraiseService.queryListbyId(user));
		Userresult.setMsg(user.getRole().getRoleName());

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}

	public String loadAdminseeList() {
		Result<adminResults> Userresult = new Result<adminResults>();

		Userresult.setItems(appraiseService.initAdminSeeList());

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}

	@SuppressWarnings("unchecked")
	public String loadTcherClassList() {
		Result<Appraise> Userresult = new Result<Appraise>();

		Userresult.setItems(appraiseService.queryListbyInfo(major, banji, cname));
		Userresult.setUrl("AppraiseList");

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}

	public String loadTcherClassList1() {
		Result<Appraise> Userresult = new Result<Appraise>();
		Appraise appraise = appraiseService.findAppraise(Integer.parseInt(id));
		Userresult.setItems(appraiseService.queryListbyInfo(appraise.getStudent().getMajor(), appraise.getStudent().getBanji(), appraise.getSubject().getsName()));
		Userresult.setUrl("AppraiseList");

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}

	// 查找返回评价表
	public String findAppraise() {
		Result<Appraise> Userresult = new Result<Appraise>();

		Userresult.setItem(appraiseService.findAppraise(Integer.parseInt(id)));

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}

	// 写入教师的回复到数据库
	public String saveReply() {
		Result<Appraise> Userresult = new Result<Appraise>();

		Appraise appraise = appraiseService.findAppraise(Integer.parseInt(id));
		appraise.setReply(reply);

		appraiseService.updateAppraise(appraise);

		Userresult.setMsg("回复成功！");

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}
}
